package fr.upmc.i3s.upmcontact.views.fragments;

import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.backend.BackEndBridgeImpl;
import fr.upmc.i3s.upmcontact.contents.User;
import fr.upmc.i3s.upmcontact.tools.BitmapFactoryAdapter;
import fr.upmc.i3s.upmcontact.tools.StringUtils;
import fr.upmc.i3s.upmcontact.views.FragmentBaseActivity;
import fr.upmc.i3s.upmcontact.views.FragmentController;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("DefaultLocale")
public class MenuFragment extends ListFragment {
	
	private static final int NB_ROW_MENU = 9;
	
	private static final int ACCUEIL = 0;
	private static final int INFOS_UPMC = 1;
	private static final int COMMERCANTS = 2;
	private static final int VIE_ETUDIANTE = 3;
	private static final int INCIDENTS = 4;
	private static final int OBJETS_PERDUS = 5;
	private static final int INFOS_UTILES = 6;
	private static final int CHAT_ETUDIANT = 7;
	private static final int PARAMETRES = 8;
	
	private SlideArrayAdapter menuAdapter;
	
	private TextView tvUsername;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		this.tvUsername = (TextView) ((FragmentBaseActivity) getActivity()).getSlidingMenu().findViewById(R.id.textView_username);
		User user = BackEndBridgeImpl.getInstance(getActivity()).getCurrentUser();
		if(user!=null) {
			String username = BackEndBridgeImpl.getInstance(getActivity()).getCurrentUser().getUsername();
			this.tvUsername.setText(StringUtils.toProperNoun(username));
		} else {
			this.tvUsername.setText(R.string.unknown);
		}

		
		String[] menu_content = getResources().getStringArray(R.array.sliding_menu_content_names);
		
		int[] menu_icons = { 	R.drawable.ic_menu_home_white,
								R.drawable.ic_menu_university_white,
								R.drawable.ic_menu_merchant_white,
								R.drawable.ic_menu_student_white,
								R.drawable.ic_menu_incident_white,
								R.drawable.ic_menu_found_white,
								R.drawable.ic_menu_info_white,
								R.drawable.ic_menu_chat_white,
								R.drawable.ic_menu_setting_white
		};
		
		menuAdapter = new SlideArrayAdapter(getActivity());
		for(int i=0; i<NB_ROW_MENU; i++) {
			menuAdapter.add(new MenuRow(menu_content[i], menu_icons[i]));
		}
		
		setListAdapter(menuAdapter);
	}
	
	@Override
	public void onListItemClick(ListView lv, View v, int position, long id) {
		Fragment newContent = null;
		switch (position) {
		case ACCUEIL:
			newContent = HomeFragment.getInstance();
			break;
		case INFOS_UPMC:
			newContent = FeedUpmcInfosFragment.getInstance();
			break;
		case COMMERCANTS:
			newContent = FeedMerchantsInfosFragment.getInstance();
			break;
		case VIE_ETUDIANTE:
			newContent = FeedStudentInfosFragment.getInstance();
			break;
		case INCIDENTS:
			newContent = ReportIncidentFragment.getInstance();
			break;
		case OBJETS_PERDUS:
			newContent = FeedLostObjectsFragment.getInstance();
			break;
		case INFOS_UTILES:
			newContent = UsefulInfosFragment.getInstance();
			break;
		case CHAT_ETUDIANT:
			newContent = ChatFragment.getInstance();
			break;
		case PARAMETRES:
			newContent = SettingsFragment.getInstance();
			break;
		}
		if (newContent != null) {
			switchFragment(newContent);
		}
	}

	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;
		if (getActivity() instanceof FragmentController) {
			FragmentController fca = (FragmentController) getActivity();
			fca.switchContent(fragment);
		} 
	}
	
	
	
	///////////////////
	// ARRAY ADAPTER //
	///////////////////
	
	private class MenuRow {
		public String title;
		public int icon;
		public MenuRow(String title, int icon) {
			this.title = title; 
			this.icon = icon;
		}
	}

	public class SlideArrayAdapter extends ArrayAdapter<MenuRow> {
		public SlideArrayAdapter(Context context) {
			super(context, 0);
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
			}
			ImageView icon = (ImageView) convertView.findViewById(R.id.row_icon);
			icon.setImageBitmap(BitmapFactoryAdapter.decodeResource(getActivity(), getItem(position).icon, 60));
			TextView title = (TextView) convertView.findViewById(R.id.row_title);
			title.setText(getItem(position).title);
			return convertView;
		}

	}

}
